package com.wy.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wy.common.R;
import com.wy.controller.dto.CustomerPetDTO;
import com.wy.entity.Customer;
import com.wy.service.ICustomerService;
import com.wy.service.IPetTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.wy.service.IPetService;
import com.wy.entity.Pet;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 王昭贵
 * @since 2023-01-14
 */
@Slf4j
@RestController
@RequestMapping("/pet")
public class PetController {

    @Resource
    private IPetService petService;

    @Autowired
    private ICustomerService customerService;

    @Resource
    IPetTypeService petTypeService;

    // 新增或者更新
    @PostMapping
    public R save(@RequestBody Pet pet) {
        petService.saveOrUpdate(pet);
        return R.success("");
    }

    /****
     * 根据用户id获取宠物列表
     * @param id
     * @return
     */
    @GetMapping("/byCustomerId/{id}")
    public R getMyPetList(@PathVariable long id) {
        LambdaQueryWrapper<Pet> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Pet::getCustomerId, id).eq(Pet::getDeleteStatus,1);
        List<Pet> list = petService.list(lambdaQueryWrapper);
        CustomerPetDTO customerPetDTO = new CustomerPetDTO();
        customerPetDTO.setPetList(list);
        customerPetDTO.setCount(list.size());
        log.info("当前getMyPetList====》{}", list);
        return R.success(customerPetDTO);
    }

    /****
     * 逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id) {
        Pet pet = petService.getById(id);
        pet.setDeleteStatus(0);
        petService.updateById(pet);
        return R.success("删除成功");
    }



    /****
     * 后台宠物管理--宠物列表分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R findPage(@RequestParam Integer page,
                      @RequestParam Integer pageSize,
                      @RequestParam String name) {
        LambdaQueryWrapper<Pet> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(name)) {
            queryWrapper.like(Pet::getName, name);
        }
        queryWrapper.eq(Pet::getDeleteStatus,1).orderByDesc(Pet::getId);
        Page<Pet> page1 = petService.page(new Page<>(page, pageSize), queryWrapper);
        List<Pet> petList = page1.getRecords();
        log.info("当前pet的petList===>{}",petList);
        for (Pet pet : petList) {
            if (pet.getCustomerId() != null) {
                pet.setCustomerName(customerService.getById(pet.getCustomerId()).getName());
            }
        }
        log.info("当前pet的page1===>{}",page1);
        return R.success(page1);
    }

}

